//
//  YXCustomSegmentedView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/23.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXCustomSegmentedView.h"

@implementation YXCustomSegmentedView

-(instancetype)init {
    
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        _textFont = [UIFont systemFontOfSize:13];
        _tint_Color = [UIColor blackColor];
        _selectedIndex = 0;
        _buttonArray = [NSMutableArray array];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame {
    //初始化默认数据
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        _textFont = [UIFont systemFontOfSize:13];
        _tint_Color = [UIColor blackColor];
        _selectedIndex = 0;
        _buttonArray = [NSMutableArray array];
    }
    return self;
}

static int tagIndex = 151515;
-(void)setTitleArray:(NSArray *)titleArray {
    
    
    _titleArray = titleArray;
    CGFloat width = self.frame.size.width / titleArray.count;
    for (int i = 0; i < _titleArray.count; i++) {
        NSString *string = _titleArray[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(width * i, 0, width, self.frame.size.height);
        button.tag = tagIndex + i;
        button.titleLabel.font = self.textFont;
        [button setTitle:string forState:UIControlStateNormal];
        [button setTitle:string forState:UIControlStateSelected];
        [button setTitleColor:self.tint_Color forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor]forState:UIControlStateSelected];
        [button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = _tint_Color.CGColor;
        if (i == self.selectedIndex) {
            button.selected = YES;
            self.selectedButton = button;
            [button setBackgroundColor:self.tint_Color];
        }
        [self.buttonArray addObject:button];
        [self addSubview:button];
    }
}

-(void)setTint_Color:(UIColor *)tint_Color {
    
    _tint_Color = tint_Color;
    for (UIButton *button in self.subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            [button setTitleColor:tint_Color forState:UIControlStateNormal];
            button.layer.borderColor = tint_Color.CGColor;
        }
        if (button.selected == YES) {
            [button setBackgroundColor:tint_Color];
        }
    }
}

-(void)setTextFont:(UIFont *)textFont {
    
    _textFont = textFont;
    for (UIButton *button in self.subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            button.titleLabel.font = textFont;
        }
    }
}

-(void)setSelectedIndex:(NSInteger)selectedIndex {
    
    _selectedIndex = selectedIndex;
    [self didClickButton:self.buttonArray[selectedIndex]];
}

-(void)didClickButton:(UIButton *)button {
    
    _selectedIndex = button.tag - tagIndex;
    [self.selectedButton setBackgroundColor:[UIColor whiteColor]];
    self.selectedButton.selected = NO;
    self.selectedButton = button;
    self.selectedButton.selected = YES;
    [self.selectedButton setBackgroundColor:self.tint_Color];
    if (!self.delegate || !_action) {
        return;
    }
    [self.delegate performSelector:_action withObject:self];
}

-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)event{
    
    if (event == UIControlEventValueChanged) {
        self.delegate = target;
        self.action = action;
    }
}


@end
